package cn.edu.tju.elm.repository;

import cn.edu.tju.elm.model.WalletRule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface WalletRuleRepository extends JpaRepository<WalletRule, Long> {

    /**
     * 根据规则类型查找启用的规则，按优先级降序排列
     * @param ruleType 规则类型
     * @return 规则列表
     */
    List<WalletRule> findByRuleTypeAndIsEnabledTrueOrderByPriorityDesc(String ruleType);

    /**
     * 查找适用于指定金额和类型的规则
     * @param ruleType 规则类型
     * @param amount 金额
     * @return 规则列表
     */
    @Query("SELECT r FROM WalletRule r WHERE r.ruleType = :ruleType AND r.isEnabled = true " +
           "AND r.minAmount <= :amount AND (r.maxAmount IS NULL OR r.maxAmount >= :amount) " +
           "ORDER BY r.priority DESC")
    List<WalletRule> findApplicableRules(@Param("ruleType") String ruleType, @Param("amount") BigDecimal amount);

    /**
     * 查找所有启用的规则
     * @return 规则列表
     */
    List<WalletRule> findByIsEnabledTrue();
}

